﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Diagnostics;

namespace Lilium.Web.Mvc.Metadata
{
	/// <summary>
	/// Расширение метаданных, используемое для хранения информации об ассоциациях (внешние ключи в базах данных)
	/// </summary>
	[Obsolete("по крайней мере до тех пор, пока получение метаданных не будет унифицированно")]
	public class AssociationMetadataExtension
	{
		public AssociationMetadataExtension(
			Type referencedType, bool isForeignKey, string thisKey, string otherKey, string otherName, string otherOrder)
		{
			if (referencedType == null) throw new ArgumentNullException("referencedType");

			m_IsForeignKey = isForeignKey;
			m_OtherKey = otherKey;
			m_OtherName = otherName;
			m_OtherOrder = otherOrder;
			m_ThisKey = thisKey;
			m_ReferencedType = referencedType;
		}

		public string GetOtherText(object other)
		{
			if (other != null)
			{
				if (!string.IsNullOrEmpty(OtherName))
					return string.Format("{0}", ReferencedType.GetProperty(OtherName).GetValue(other, null));
				else
					return string.Format("{0}", other);
			}
			else
				return null;
		}

		#region public bool IsForeignKey

		private readonly bool m_IsForeignKey;

		public bool IsForeignKey
		{
			[DebuggerNonUserCodeAttribute]
			get
			{
				return m_IsForeignKey;
			}
		}

		#endregion

		#region public string OtherKey

		private readonly string m_OtherKey;

		public string OtherKey
		{
			[DebuggerNonUserCodeAttribute]
			get
			{
				return m_OtherKey;
			}
		}

		#endregion

		#region public string OtherName

		private readonly string m_OtherName;

		public string OtherName
		{
			[DebuggerNonUserCodeAttribute]
			get
			{
				return m_OtherName;
			}
		}

		#endregion

		#region public string OtherOrder

		private readonly string m_OtherOrder;

		public string OtherOrder
		{
			[DebuggerNonUserCodeAttribute]
			get
			{
				return m_OtherOrder;
			}
		}

		#endregion

		#region public string ThisKey

		private readonly string m_ThisKey;

		public string ThisKey
		{
			[DebuggerNonUserCodeAttribute]
			get
			{
				return m_ThisKey;
			}
		}

		#endregion

		#region public Type ReferencedType

		private readonly Type m_ReferencedType;

		public Type ReferencedType
		{
			[DebuggerNonUserCodeAttribute]
			get
			{
				return m_ReferencedType;
			}
		}

		#endregion
	}
}